package jiaxa.com.jsbridge.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;

import jiaxa.com.jsbridge.R;
import jiaxa.com.jsbridge.util.GsonUtil;
import jiaxa.com.jsbridge.util.ImageUtil;

@ContentView(R.layout.activity_photo)
public class PhotoActivity extends TakePhotoActivity {

    //    @ViewInject(R.id.btnPhoto)
    //    private Button btnPhoto;
    //    @ViewInject(R.id.btnAlbum)
    //    private Button btnAlbum;
    @ViewInject(R.id.imgView)
    private ImageView imgView;

    private TakePhoto takePhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);


        takePhoto = getTakePhoto();

        new  Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(PhotoActivity.this, WelcomeActivity.class);
                startActivity(intent);
            }
        }).start();

//        Thread t = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //  Initialize SharedPreferences
//                SharedPreferences getPrefs = PreferenceManager
//                        .getDefaultSharedPreferences(getBaseContext());
//
//                //  Create a new boolean and preference and set it to true
//                boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
//
//                //  If the activity has never started before...
////                if (isFirstStart) {
//
//                    //  Launch app intro
//                    final Intent i = new Intent(PhotoActivity.this, IntroActivity.class);
//
////                    runOnUiThread(new Runnable() {
////                        @Override public void run() {
////                            startActivity(i);
////                        }
////                    });
//
//                Intent intent = new Intent(PhotoActivity.this, IntroActivity.class);
//                startActivity(intent);
//
//                //  Make a new preferences editor
//                    SharedPreferences.Editor e = getPrefs.edit();
//
//                    //  Edit preference to make it false because we don't want this to run again
//                    e.putBoolean("firstStart", false);
//
//                    //  Apply changes
//                    e.apply();
////                }
//            }
//        });
//
//        // Start the thread
//        t.start();


    }


    @Event(value = R.id.btnPhoto, type = View.OnClickListener.class)
    private void btnPhotoClickListener(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".png");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        takePhoto.onEnableCompress(getCompressConfig(), true);
        takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions());
    }

    @Event(value = R.id.btnAlbum, type = View.OnClickListener.class)
    private void btnAlbumClickListener(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".png");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        takePhoto.onEnableCompress(getCompressConfig(), true);
        takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        ArrayList<TImage> image = result.getImages();
        Log.i("PHOTO", GsonUtil.toJson(image));
        for (TImage img : image) {
            Bitmap bitmap = BitmapFactory.decodeFile(img.getCompressPath());
            imgView.setImageBitmap(bitmap);
            String base64 = ImageUtil.imageToBase64(img.getOriginalPath());
            Log.i("PHOTO", base64);
        }
    }

    private CropOptions getCropOptions() {
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setOutputX(800).setOutputY(800);
        builder.setWithOwnCrop(true);
        return builder.create();
    }

    private CompressConfig getCompressConfig() {
        CompressConfig compressConfig = new CompressConfig.Builder()
                .setMaxSize(5 * 1024)
                .setMaxPixel(500)
                .create();
        return compressConfig;
    }
}
